package com.cat.recruit.service.impl;

import com.cat.recruit.common.constant.ConstantField;
import com.cat.recruit.common.exception.BusinessException;
import com.cat.recruit.common.exception.BusinessExceptionEnum;
import com.cat.recruit.common.result.Result;
import com.cat.recruit.config.JwtUserDetails;
import com.cat.recruit.entity.User;
import com.cat.recruit.mapper.UserMapper;
import com.cat.recruit.service.JwtUserDetailsService;
import com.cat.recruit.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cat
 */
@Service
public  class JwtUserDetailsServiceImpl implements JwtUserDetailsService  {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserById(Integer id) throws AuthenticationException {
        // 根据id查询用户信息
        User user = userMapper.selectById(id);
        if (user == null){
            throw new UsernameNotFoundException("用户id不存在: " + id);
        }

        // 封装用户角色
        // 这里直接以角色名作为权限名
        Collection<GrantedAuthority> thisAuthorities = new ArrayList<>();
        thisAuthorities.add(new SimpleGrantedAuthority(user.getRole()));
        // 设置权限集合
        thisAuthorities.add(new SimpleGrantedAuthority("ROLE_"+user.getRole()));

        // 封装并返回 JwtUserDetails
        return JwtUserDetails.builder()
                .username(user.getName())
                .authorities(thisAuthorities)
                .role(user.getRole())
                .id(user.getUserId())
                .password(user.getPassword())
                .build();
    }

    /**
     * 生成JWT
     *
     * @param user 用户信息
     */
    @Override
    public  String generateToken(User user) {
        // 构造载体
        Map<String, Object> claims = new HashMap<>();
        claims.put(ConstantField.ID, user.getUserId());
        claims.put(ConstantField.NAME, user.getUsername());
        claims.put(ConstantField.ROLE, user.getRole());
        claims.put(ConstantField.PERMISSION, user.getRole());

        // 生成token
        return JwtUtils.generateToken(claims);
    }

    /**
     * 生成刷新token
     */
    public  String generateRefreshToken(User user) {
        // 构造载体
        Map<String, Object> claims = new HashMap<>();
        claims.put(ConstantField.ID, user.getUserId());
        claims.put(ConstantField.NAME, user.getUsername());

        // 生成刷新token
        return JwtUtils.generateRefreshToken(claims);
    }

    /**
     * 根据refreshToken生成新的token和refreshToken
     */
    public  Result refreshToken(String refreshToken) {
        // 解析refreshToken
        Claims claims = JwtUtils.parseRefreshToken(refreshToken);
        return Result.success(generateTokenAndRefreshToken(claims.get(ConstantField.ID, Integer.class)));
    }

    /**
     * 根据userId生成token和refreshToken
     */
    public  Map<String, String> generateTokenAndRefreshToken(Integer userId) {
        // 根据id查询用户信息
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw BusinessException.bizException(BusinessExceptionEnum.USER_NOT_EXIST);
        }
        // 生成token
        String token = generateToken(user);
        // 生成refreshToken
        String refreshTokenNew = generateRefreshToken(user);
        // 返回结果
        return Map.of("token", token, "refreshToken", refreshTokenNew);
    }
}
